#include "EWAUITimelineWidget.hpp"

#include "EWAUIWidget/EWAUIWidgetHorizontalSegBar.hpp"

#include "EWAPrjTrackUI.hpp"

namespace Eyer
{
    EWAUITimelineWidget::EWAUITimelineWidget(EWAUILayoutParams width, EWAUILayoutParams height)
        : EWAUIWidget(width, height)
        , timelineCtx       (new EWAUITimelineContext())

        , ruleLayout        (new EWAUILayout(
                                            EWAUILayoutType::LinearLayoutHorizontal,
                                            LP::MATCH_PARENT,
                                            LP::WRAP_CONTENT
                                            ))
        , ruleHeaderLayout  (new EWAUILayout(
                                            EWAUILayoutType::LinearLayoutHorizontal,
                                            LP::WRAP_CONTENT,
                                            LP::WRAP_CONTENT
                                            ))
        , ruleWidget (new EWAUIRuleWidget(LP::MATCH_PARENT, LP::WRAP_CONTENT))
        , verticalScroll (new EWAUIWidgetScroll(ScrollType::VERTICAL, LT::LinearLayoutHorizontal, LP::MATCH_PARENT, LP::MATCH_PARENT))
        , trackScrollContentLayout (new EWAUITrackScrollContentLayout(timelineCtx, LP::MATCH_PARENT, LP::WRAP_CONTENT))
        , trackBodyScroll(new EWAUIWidgetScroll(ScrollType::HORIZONTAL, LT::LinearLayoutHorizontal, LP::MATCH_PARENT, LP::WRAP_CONTENT))
    {
        SetWidgetName("EWAUITimelineWidget");
        SetLayoutType(EWAUILayoutType::LinearLayoutVertical);
        ruleLayout->SetWidgetName("EWAUIRuleLayout");
        {
            ruleHeaderLayout->SetWidgetName("EWAUIRuleHeaderLayout");
            ruleHeaderLayout->SetMinWH(200, ruleWidget->ruleHeight);
            ruleHeaderLayout->SetBGColor(EyerColor::RED);
            ruleLayout->AddWidget(ruleHeaderLayout);
            ruleLayout->AddWidget(ruleWidget);
        }
        AddWidget(ruleLayout);

        verticalScroll->SetWidgetName("EWAUITrackVerticalScroll");
        {
            verticalScroll->AddContentWidget(trackScrollContentLayout);
        }
        AddWidget(verticalScroll);
    }

    EWAUITimelineWidget::~EWAUITimelineWidget()
    {

    }

    int EWAUITimelineWidget::AddTrack()
    {
        EyerSmartPtr<EWAPrjTrackUI> track(new EWAPrjTrackUI("", EWAPrjTrackType::VIDEO));
        timelineCtx->timeline.AddTrack(track, 0, EWAPrjTrackType::VIDEO);
        return 0;
    }

    int EWAUITimelineWidget::InsertFragment(const EyerSmartPtr<EWAPrjFragmentFragmentResourse> & fragment, int trackId)
    {
        if(trackId < 0){
            return -1;
        }
        if(trackId >= timelineCtx->timeline.GetTrackCount(EWAPrjTrackType::VIDEO)){
            return -2;
        }

        EyerSmartPtr<EWAPrjTrack> track = timelineCtx->timeline.GetTrack(trackId, EWAPrjTrackType::VIDEO);
        track->PushFragmentBack(fragment);

        return 0;
    }
}